﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class Sys_newsmanager : System.Web.UI.Page
{
    PM.PMSys.news thenews = new PM.PMSys.news();
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (Session["tid"] == null || Session["name"] == null || Session["sex"] == null || Session["depart"] == null || Session["usertype"] == null)
        {
            PM.Code.Web.alertmessage.ShowMsg("账号登录已超时，请返回重新登录。", "../Index.aspx");
            return;
        }
        else if (Session["usertype"].ToString()!="4")
        {
            PM.Code.Web.alertmessage.ShowMsg("不许捣乱！罚你回去重新登录~~~~", "../Index.aspx");
            return;
        } 
        else  if (!IsPostBack)
        {
            gridbind();
        }
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        gridbind();
    }

     //数据绑定
    protected void gridbind()
    {
            StringBuilder sqlstrfield = new StringBuilder();
            StringBuilder sqlstrwhere = new StringBuilder();
            getField(ref sqlstrfield);
            getWhere(ref sqlstrwhere);
            PM.Code.Web.controlbind.BindGridViewPublicProc(GridView1, AspNetPager1, "Sys_News", "id", sqlstrfield.ToString(), "date desc", sqlstrwhere.ToString(), true);
    }
    //设置查询字段
    protected void getField(ref StringBuilder sqlstrfield)
    {
        sqlstrfield.Append("id,type,title,date,case highlight when 1 then '是' when 0 then '否' end as ishighlight");
    }
    //设置查询条件
    protected void getWhere(ref StringBuilder sqlstrwhere)
    {
        sqlstrwhere.Append("1=1");
        if (ddlType.SelectedIndex != 0)
        {
            sqlstrwhere.AppendFormat(" and type='{0}'", ddlType.SelectedValue.ToString());
        }
        if (txtTitlekeywords.Text.ToString().Trim() != "")
        {
            sqlstrwhere.AppendFormat(" and title like '%{0}%' ", txtTitlekeywords.Text.ToString().Trim());
        }
        if (txtContentkeywords.Text.ToString().Trim() != "")
        {
            sqlstrwhere.AppendFormat(" and newscontent like '%{0}%' ", txtContentkeywords.Text.ToString().Trim());
        }
    }

    //截取多余字符
    static public string strTrim(string str)
    {
        string tmpStr;
        tmpStr = PM.Code.Basic.pmbasic.strCal(str, 50);
        return tmpStr;
    }
    //将数据库中的长时间转变成短时间
    static public string ToShortTime(string time)
    {
        string date;
        date = (Convert.ToDateTime(time)).ToShortDateString();
        return date;
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string id = e.CommandArgument.ToString();
        if (e.CommandName == "Highlight")
        {
            int a=thenews.doHighlight(int.Parse(id));
            if (a==1)
            {
                gridbind();
            }           
        } 
        else if (e.CommandName=="NewsEdit")
        {
            Response.Redirect("newswriting.aspx?id=" + id);  
        }
        else if (e.CommandName=="NewsDelete")
        {
            if (thenews.Delete(int.Parse(id)))
            {
                gridbind();
            }
            else
            {
                PM.Code.Web.alertmessage.ShowMsg(UpdatePanel1, "未知原因导致删除失败，请重试！");
            }
        }
    }
    protected void btnSerch_Click(object sender, ImageClickEventArgs e)
    {
        gridbind();
    }
}
